<template>
  <ex-dialog
    width="700px"
    class="ex-diolog"
    :title="title"
    :visible="visible"
    :close-on-click-modal="false"
    @closeDialog="handleClose"
  >
    <div class="main" v-loading="loading">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm" :disabled="disabled">
        <el-row>
          <el-col :span="18">
            <el-form-item label="视频号商品id：" prop="videoProductId">
              <el-input v-model="ruleForm.videoProductId" placeholder="请填写视频号商品id"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="选择赚卡：" prop="goodsId">
              <el-select v-model="ruleForm.goodsId" filterable placeholder="请填写赚卡名称" size="small" >
                <el-option v-for="item in cardList" :key="item.id" :label="item.value" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="button_box">
      <el-button @click="handleClose">取消</el-button>
      <el-button
        type="primary"
        :loading="AgreeLoading"
        @click="submitForm('ruleForm')"
      >保存提交</el-button>
    </div>
  </ex-dialog>
</template>
<script>
import Loading from "@/components/Loading/index";
// api
import {
  addProduct,
  selectList
} from "@/api/merchantsNFR/wxGoodsManage";
export default {
  components: {
    Loading,
  },
  props: {
    visible: Boolean,
  },
  data() {
    return {
      labelWidth: "150px",
      inputWidth: "420px",
      title: "新建",
      loading: false,
      disabled: false,
      AgreeLoading: false,
      ruleForm: {
        videoProductId: '',//视频号商品id
        goodsId: '',//平台赚卡商品id
      },
    
      cardList: [],//赚卡下拉列表
      rules: {
        videoProductId: [
          { required: true,message: "请填写视频号商品id",trigger: ["change", "bulr"]}
        ],
        goodsId: [
          { required: true,message: "请选择赚卡",trigger: ["change" ]}
        ],
       
      },
    };
  },
  computed: {},
  created() {
    this.getCard()
    
  },
  mounted() {
   
  },
  computed: {},
  methods: {
    // 获取赚卡
    getCard(){
      selectList().then(res => {
        console.log('res.result',res.result)
        this.cardList = res.result
      })
    },
    //取消
    handleClose(formName) {
      this.$emit("update:visible", false);
    },
    submitForm(formName) {
      console.log(this.ruleForm);
      //创建确定
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.AgreeLoading = true
          var data = {
            ...this.ruleForm
          }
          addProduct(data).then(res => {
            this.handleClose()
            this.$message.success('创建成功')
            this.$emit('success')
            this.AgreeLoading = false
          }).finally(()=>{
            this.AgreeLoading = false
          })
          
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  }
};
</script>
<style lang="scss" scoped>
.ex-diolog {
  ::v-deep .ex--dialog--footer {
    border: none !important;
    padding: 0 !important;
  }

  ::v-deep .ex--dialog--main {
    padding: 10px 20px !important;
  }
}
.el-select,.el-input{
  width: 300px;
}
.main {
  padding: 20px 20px;
}
.button_box{
  display: flex;
  justify-content: center;
}
</style>
